package love.xin.util

import android.annotation.SuppressLint
import android.app.AlarmManager
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import love.xin.ui.DiaryActivity
import love.xin.R
import love.xin.receiver.AlarmReceiver
import java.util.Calendar

class BaseUtil {
    companion object{

        @SuppressLint("ScheduleExactAlarm")
        fun setTime(context: Context){


            val calendar = Calendar.getInstance().apply {
                add(Calendar.MINUTE, 5) // 设置一分钟后
            }

            val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
            val intent = Intent(context, AlarmReceiver::class.java)
            val pendingIntent = PendingIntent.getBroadcast(
                context,
                0,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
            )
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent)
        }
        @SuppressLint("MissingPermission")
         fun createNotification(context:Context) {

            val channelId = "my_channel_id"
            val channelName = "My Channel"
            val channelDescription = "My Channel Description"
            val importance = NotificationManager.IMPORTANCE_DEFAULT

            val notificationChannel = NotificationChannel(channelId, channelName, importance)
            notificationChannel.description = channelDescription

            val notificationManager =
                context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(notificationChannel)


            // 创建通知
            val notificationId = 1
            val builder = NotificationCompat.Builder(context, "my_channel_id")
                .setSmallIcon(R.drawable.ic_launcher_background) // 通知图标
                .setContentTitle("Notification Title") // 通知标题
                .setContentText("Notification Content") // 通知内容
                .setPriority(NotificationCompat.PRIORITY_DEFAULT) // 通知优先级
            val intent = Intent(context, DiaryActivity::class.java).apply {
                flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
            }
            val pendingIntent = PendingIntent.getActivity(
                context,
                0,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE // 指定 FLAG_IMMUTABLE
            )
            builder.setContentIntent(pendingIntent)
            builder.setAutoCancel(true) // 点击通知后自动取消
            // 显示通知
            with(NotificationManagerCompat.from(context)) {
                notify(notificationId, builder.build())
            }
        }
    }
}